package com.example.xianji.adapter;

import java.util.ArrayList;

import com.example.xianji.R;
import com.example.xianji.eventbus.FirstEventBus;
import com.example.xianji.bean.Place;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

public class PlaceAdapter extends BaseAdapter {
    /***********************服务大厅  ：分类的三级列表适配器******************************/
    private Context mContext;
    private ArrayList<Place> place_list;
    private int selected_position = -1;
    private int list_from;

    public PlaceAdapter(Context mContext, ArrayList<Place> place_list, int list_from) {
        super();
        this.mContext = mContext;
        this.place_list = place_list;
        this.list_from = list_from;
    }

    @Override
    public int getCount() {
        return place_list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = View.inflate(mContext, R.layout.listview_item_layout, null);
            holder.place_text = (TextView) convertView.findViewById(R.id.place_text);
            holder.linear_layout = (LinearLayout) convertView.findViewById(R.id.linear_layout);
            convertView.setTag(holder);
        }
        holder = (Holder) convertView.getTag();
        holder.linear_layout.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        if (selected_position == position) {
            holder.linear_layout.setBackgroundColor(mContext.getResources().getColor(R.color.black6));
        } else {
            holder.linear_layout.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }

        holder.place_text.setText(place_list.get(position).getArea_name());
        if (list_from == 1) {
            final int a = position;
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EventBus.getDefault().postSticky(new FirstEventBus(true, place_list.get(a).getArea_name(), place_list.get(a).getArea_id()));
//				EventBus.getDefault().post(new FirstEventBus(true,place_list.get(a).getArea_name(),place_list.get(a).getArea_id()));
                }
            });
        }
        return convertView;
    }

    class Holder {
        TextView place_text;
        LinearLayout linear_layout;
    }

    public void set_selectposition(int position) {
        selected_position = position;
    }
}
